package com.magicborrow.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;

import java.util.ArrayList;

/**
 * Created by River on 7/24/15.
 */
public class ForceRefreshListFragmentAdapter extends FragmentPagerAdapter{

    ArrayList<Fragment> mfragments;
    FragmentManager fm;

    public void setData(ArrayList<Fragment> fragments){
        if(mfragments != null){
            FragmentTransaction ft = fm.beginTransaction();
            for (Fragment f:this.mfragments) {
                ft.remove(f);
            }
            ft.commit();
            ft = null;
            fm.executePendingTransactions();
        }
        this.mfragments = fragments;
        notifyDataSetChanged();
    }

    public ForceRefreshListFragmentAdapter(FragmentManager fm) {
        super(fm);
        this.fm = fm;
    }

    @Override
    public Fragment getItem(int position) {
        return mfragments.get(position);
    }

    @Override
    public int getCount() {
        return mfragments.size();
    }
}
